package edu.pitt.myptm;

public class WriteCommand extends MyCommand {
	public int getType() { return MyCommand.WRITE;}
	public boolean isWriteCommand() {
		return true;
	}
	private String filename;
	private DataItem item = new DataItem(); 
	
	public void setFilename(String filename) {
		this.filename = filename;
	}
	
	public String getFilename() {
		return this.filename;
	}
	
	public void setDataItem(String str) {
		String[] subs = str.split(",");
		item.id = Integer.parseInt(subs[0]);
		
		int clientLength = item.clientLength;
		byte[] clientbytes = subs[1].getBytes();
		if(clientLength>clientbytes.length) clientLength = clientbytes.length;
		for(int i = 0;i<clientLength;i++) {
			item.clientName[i] = clientbytes[i];
		}
		
		int phoneLength = item.phoneLength;
		byte[] phonebytes = subs[2].getBytes();
		if(phoneLength>phonebytes.length) phoneLength = phonebytes.length;
		for(int i= 0;i<phoneLength;i++) {
			item.phone[i] = phonebytes[i];
		}
	}
	
	public DataItem getDataItem() {
		return this.item;
	}
	
	public String toString() {
		return this.getTransactionId()+": WRITE "+filename + " "+item.toString();
	}
}
